package com.controller;

import com.mapper.TeacherMapper;
import com.pojo.Teacher;
import com.pojo.Team;
import com.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 教师
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    TeacherMapper tm;

    /**
     * 分页获取教师列表
     *
     * @param page     页码
     * @param pagesize 单页数据量
     */
    @RequestMapping("/getList")
    public Map<String, Object> getList(Integer page, Integer pagesize) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (page == null) {
            code = 500;
            msg = "page 不能为空";
        } else if (pagesize == null) {
            code = 501;
            msg = "pagesize 不能为空";
        } else {
            page *= pagesize;
            List<Teacher> list = tm.getList(page, pagesize);
            for (Teacher teacher : list) {
                teacher.setPassword(null);
                teacher.setStation_name(tm.getStationById(teacher.getStation_id()).getName());
            }
            map.put("total", tm.countTeacher());
            map.put("data", list);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 添加职务
     *
     * @param name 职务名
     */
    @RequestMapping("/addStation")
    public Map<String, Object> addStation(String name) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (name == null) {
            code = 500;
            msg = "name 不能为空";
        } else if (tm.getStationByName(name) != null) {
            code = 501;
            msg = "职务已存在";
        } else {
            tm.addStation(name);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 删除职务
     *
     * @param id 职务 id
     */
    @RequestMapping("/delStation")
    public Map<String, Object> delStation(Integer id) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (tm.getStationById(id) == null) {
            code = 501;
            msg = "职务不存在";
        } else {
            tm.delStation(id);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 修改职务
     *
     * @param id   职务 id
     * @param name 职务名
     */
    @RequestMapping("/editStation")
    public Map<String, Object> editStation(Integer id, String name) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (name == null) {
            code = 501;
            msg = "name 不能为空";
        } else if (tm.getStationById(id) == null) {
            code = 502;
            msg = "职务 id 不存在";
        } else if (tm.getStationByName(name) != null) {
            code = 503;
            msg = "职务名已存在";
        } else {
            tm.editStation(id, name);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 获取职务列表
     */
    @RequestMapping("/getStations")
    public Map<String, Object> getStations() {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        map.put("code", code);
        map.put("msg", msg);
        map.put("data", tm.getStations());
        return map;
    }

    /**
     * 注册教师
     *
     * @param station_id 职称 id
     * @param gender     性别
     * @param jobnum     职工号
     * @param name       姓名
     * @param account    手机号
     * @param introduce  介绍
     * @param avatar     头像
     */
    @RequestMapping("/addTeacher")
    public Map<String, Object> addTeacher(Integer station_id, Integer gender, String jobnum, String name, String account, String introduce, String avatar) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (station_id == null) {
            code = 501;
            msg = "station_id 不能为空";
        } else if (jobnum == null) {
            code = 502;
            msg = "jobnum 不能为空";
        } else if (name == null) {
            code = 503;
            msg = "name 不能为空";
        } else if (account == null) {
            code = 504;
            msg = "account 不能为空";
        } else if (tm.getByJobnum(jobnum) != null) {
            code = 506;
            msg = "职工号已存在";
        } else if (tm.getByAccount(account) != null) {
            code = 507;
            msg = "账号已存在";
        } else {
            String password = Tool.generateMd5(jobnum.substring(jobnum.length() - 4) + account.substring(account.length() - 4));    // 设置默认密码：工号后四位 + 手机号后四位
            if (avatar == null) {
                avatar = "https://thirdwx.qlogo.cn/mmopen/vi_32/wqHCib9DvYw6PCjGIuOkREBsMpmibiblN9FjPdcwDj5IVggH97iapqpFdC7SpEqqVUeoR09fzhJDnfQSZbiaN0iajIbH4Wxdds1Bib0ed6lq91fEFQ/132";
            }
            tm.addTeacher(station_id, gender, jobnum, name, account, password, introduce, avatar);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 注销教师
     *
     * @param id 教师 id
     */
    @RequestMapping("/delTeacher")
    public Map<String, Object> delTeacher(Integer id) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (tm.getById(id) == null) {
            code = 501;
            msg = "教师不存在";
        } else {
            tm.delTeacher(id);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 修改教师信息
     *
     * @param id        教师 id
     * @param type      字段编号 0-头像 1-简介 2-密码
     * @param avatar    头像
     * @param introduce 简介
     * @param password  密码
     */
    @RequestMapping("/editInfo")
    public Map<String, Object> editInfo(Integer id, Integer type, String avatar, String introduce, String password) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (type == null || type < 0 || type > 2) {
            code = 501;
            msg = "type 错误";
        } else if (avatar == null && type == 0) {
            code = 502;
            msg = "avatar 不能为空";
        } else if (introduce == null && type == 1) {
            code = 503;
            msg = "introduce 不能为空";
        } else if (password == null && type == 2) {
            code = 504;
            msg = "password 不能为空";
        } else if (tm.getById(id) == null) {
            code = 505;
            msg = "id 不存在";
        } else {
            switch (type) {
                case 0:
                    tm.editAvatar(id, avatar);
                    break;
                case 1:
                    tm.editIntroduce(id, introduce);
                    break;
                case 2:
                    password = Tool.generateMd5(password);
                    tm.editPassword(id, password);
                    break;
            }
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 获取课题预约列表
     *
     * @param page       页码
     * @param pagesize   单数数据量
     * @param teacher_id 教师 id
     */
    @RequestMapping("/getSubscribe")
    public Map<String, Object> getSubscribe(Integer page, Integer pagesize, Integer teacher_id) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (teacher_id == null) {
            code = 500;
            msg = "teacher_id 不能为空";
        } else {
            if (page == null) page = 0;
            if (pagesize == null) pagesize = 10;
            page *= pagesize;
            List<Team> list = tm.getSubscribe(teacher_id, page, pagesize);
            for (Team item : list) {
                item.setLeader_name(tm.getStudentById(item.getLeader_id()).getNickname());
                item.setTopic(tm.getTopicById(item.getTopic_id()));
                item.setTeam(tm.getTeamById(item.getId(), item.getLeader_id()));
            }
            map.put("data", list);
            map.put("total", tm.countSubscribe(teacher_id));
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 审核学生预约
     *
     * @param teacher_id 指导教师 id
     * @param team_id    小组 id
     * @param state      审核结果 1-通过 2-拒绝
     */
    @RequestMapping("/toExamine")
    public Map<String, Object> toExamine(Integer teacher_id, Integer team_id, Integer state) {
        Map<String, Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (teacher_id == null) {
            code = 500;
            msg = "指导教师 id 不能为空";
        } else if (team_id == null) {
            code = 501;
            msg = "team_id 不能为空";
        } else if (state == null) {
            code = 502;
            msg = "state 不能为空";
        } else if (tm.getTopicState(team_id) != 0) {
            code = 503;
            msg = "该审核记录不存在";
        } else {
            tm.toExamine(team_id, state);
            if (state == 1) tm.updateStudentTeacher(team_id, teacher_id);
        }

        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

}
